import { PrismaClient } from '../../src/generated/prisma';
import { readdirSync } from 'fs';
import { join } from 'path';

const prisma = new PrismaClient();

/* 自动扫描同级目录下 **\/*.seed.ts 并实例化 default 导出 */
async function runAll() {
  console.log('🌱 开始执行种子脚本...\n');
  const seedDir = __dirname;
  const files = readdirSync(seedDir, { withFileTypes: true })
  .filter(d => d.isDirectory())
  .flatMap(dir =>
    readdirSync(join(seedDir, dir.name), { withFileTypes: true })
    .filter(f => f.name.endsWith('.seed.ts'))
    .map(f => join(seedDir, dir.name, f.name)),
  );

  for (const file of files) {
    const { default: SeedClass } = await import(file);
    const seeder = new SeedClass();
    await seeder.run(prisma);
  }
  console.log('\n✅ 所有模块 seed 完成');
}

if (require.main === module) {
  runAll()
  .catch(e => {
    console.error(e)
    process.exit(1)
  })
  .finally(() => prisma.$disconnect());
}
